package falcon.common;

public final class FalconUser {
	
	public static final class Permission {
		public static final String SYS = "sys";
		public static final String BASH = "bash";
		public static final String FUNC = "func";
		public static final String FLOW = "flow";
	}

	public static int init(FalconConfig config) {
		String path = FalconStore.getPath(NAME, config);
		if (!TextFile.exists(path)) {
			return initMaster(path);
		}
		/*String[] lines = TextFile.readLines(path);
		if (lines == null) {
			return Errno.COM_READ_FILE_ERROR;
		}*/
		return Errno.SUCCESS;
	}
	
	public static int check(FalconConfig config, String user, String password, String host) {
		String path = FalconStore.getPath(NAME, config);
		String[] lines = TextFile.readLines(path);
		if (lines == null) {
			Log.log("conf not found;path=%s;user=%s", path, user);
			return Errno.COM_READ_FILE_ERROR;
		}
		String line = find(lines, user, host);
		if (line == null) {
			Log.log("user not found;path=%s;user=%s", path, user);
			return Errno.VERIF_HOST_FORBIDDEN;
		}
		String[] array = line.split(" ");
		if (array.length != 3) {
			Log.log("array.length != 3;path=%s;user=%s", path, user);
			return Errno.COM_ARGS_INVALID;
		}
		String h = array[1];
		String c = array[2];
		String[] keyArray = new String[3];
		keyArray[0] = Md5.get(path);
		keyArray[1] = Md5.get(user);
		keyArray[2] = Md5.get(h);
		String p = Security.decrypt(keyArray, c);
		if (p == null) {
			Log.log("decrypt failure;path=%s;user=%s", path, user);
			return Errno.VERIF_PASSWORD_ERROR;
		}
		if (!password.equals(Md5.get(p))) {
			Log.log("password error;path=%s;user=%s", path, user);
			return Errno.VERIF_PASSWORD_ERROR;
		}
		return Errno.SUCCESS;
	}
	
	public static int initMaster(String path) {
		char[] chs = new char[70];
		for (int i = 0; i < 10; i ++) {
			chs[i] = (char) ('0' + i);
		}
		for (int i = 10; i < 10 + 26; i ++) {
			chs[i] = (char) (i - 10 + 'a');
			chs[i + 26] = (char) (i - 10 + 'A');
		}
		chs[62] = '=';
		chs[63] = '+';
		chs[64] = '-';
		chs[65] = '*';
		chs[66] = '/';
		chs[67] = '%';
		chs[68] = '&';
		chs[69] = '(';
		StringBuilder sb = new StringBuilder(16);
		for (int i = 0; i < 8; i ++) {
			int idx = (int) (Math.random() * chs.length);
			sb.append(chs[idx]);
		}
		String user = "falcon";
		String password = sb.toString();
		Log.log("init master;user=%s;password=%s", user, password);
		return create(path, user, password, ".*");
	}
	
	public static int create(String path, String user, String password, String host) {
		StringBuilder sb = new StringBuilder(1024);
		sb.append(user);
		sb.append(' ');
		sb.append(host);
		sb.append(' ');
		String[] keyArray = new String[3];
		keyArray[0] = Md5.get(path);
		keyArray[1] = Md5.get(user);
		keyArray[2] = Md5.get(host);
		String cipher = Security.encrypt(keyArray, password);
		if (cipher == null) {
			Log.log("encrypt failure;path=%s;user=%s", path, user);
			return Errno.COM_CATCH_EXCEPTION;
		}
		sb.append(cipher);
		sb.append('\n');
		return TextFile.write(path, sb.toString(), true);
	}
	
	private static String find(String[] lines, String user, String host) {
		for (int i = 0; i < lines.length; i ++) {
			String line = lines[i];
			String[] array = line.split(" ");
			if (array.length != 3) {
				continue;
			}
			String u = array[0];
			if (!user.equals(u)) {
				continue;
			}
			String h = array[1];
			if (!host.matches(h)) {
				Log.log("host not match;user=%s;host=%s;h=%s", user, host, h);
				continue;
			}
			return line;
		}
		return null;
	}
	
	private static final String NAME = "user.conf";
}
